package org.jtb.quickr;

import java.util.Properties;
import java.util.Set;
import org.apache.log4j.Logger;

/**
 * Quickr Photos service.
 */
public class PhotosService extends Service {
    private static Logger logger = Logger.getLogger(PhotosService.class.getName());
    
    /**
     * Construct a new quickr photos service. Load quickr.properties from the class path.
     * @throws QuickrException If there was a problem constructing this object.
     */
    public PhotosService() throws QuickrException {
        super();
    }
    
    /**
     * Construct a new quickr photos service based on the given properties.
     * @param properties The quickr properties.
     * @throws QuickrException If a problem occurs constructing this object.
     */
    public PhotosService(Properties properties) throws QuickrException {
        super(properties);
    }
    
    /**
     * Delete a photo.
     * For details, see the 
     * <a href="http://www.flickr.com/services/api/flickr.photos.delete.html">
     * flickr.photos.delete service</a> documentation.
     * @param authToken The auth token.
     * @param photoId The ID of the photo to delete.
     * @throws QuickrException If a problem occurs.
     */
    public void delete(AuthResponse.Token authToken, PhotoIdResponse photoId) throws QuickrException {
        PhotosDeleteRequest pdr = new PhotosDeleteRequest(properties.getRestUrl(), properties.getApiKey(), properties.getSecret(), authToken, photoId);
        pdr.execute();
    }

    /**
     * Search for photos. This method will only return public photos.
     * For details, see the
     * <a href="http://www.flickr.com/services/api/flickr.photos.search.html">
     * flickr.photos.search service</a> documentation.
     * @param criteria The photo search criteria.
     * @return The photos response.
     * @throws QuickrException If a problem occurs.
     */
    public PhotosResponse search(PhotosSearchCriteria criteria) throws QuickrException {
        PhotosSearchRequest psr = new PhotosSearchRequest(properties.getRestUrl(), properties.getApiKey(), null, null, criteria, properties.getStaticUrl());
        PhotosResponse photos = psr.execute();
        
        return photos;
    }

    /**
     * Search for photos. This method will return private photos for which the 
     * given token grants read access.
     * For details, see the
     * <a href="http://www.flickr.com/services/api/flickr.photos.search.html">
     * flickr.photos.search service</a> documentation.
     * @param authToken The auth token.
     * @param criteria The photo search criteria.
     * @return The photos response.
     * @throws QuickrException If a problem occurs.
     */
    public PhotosResponse search(AuthResponse.Token authToken, PhotosSearchCriteria criteria) throws QuickrException {
        PhotosSearchRequest psr = new PhotosSearchRequest(properties.getRestUrl(), properties.getApiKey(), properties.getSecret(), authToken, criteria, properties.getStaticUrl());
        PhotosResponse photos = psr.execute();
        
        return photos;
    }
    
    /**
     * Get detailed information for a photo. 
     * The photo secret argument allows access to non-public photos.
     * For details, see the
     * <a href="http://www.flickr.com/services/api/flickr.photos.getInfo.html">
     * flickr.photos.getInfo service</a> documentation.
     * @param photoId The photo ID.
     * @param photoSecret The photo secret.
     * @throws QuickrException If there was a problem.
     * @return The photo response.
     */
    public PhotoResponse getInfo(String photoId, String photoSecret) throws QuickrException {
        PhotosGetInfoRequest pgir = new PhotosGetInfoRequest(properties.getRestUrl(), properties.getApiKey(), photoId, photoSecret, properties.getStaticUrl());
        PhotoResponse photo = pgir.execute();
        
        return photo;
    }    

    /**
     * Get detailed information for a photo. 
     * The photo ID argument must specify a publicly readable photo.
     * For details, see the
     * <a href="http://www.flickr.com/services/api/flickr.photos.getInfo.html">
     * flickr.photos.getInfo service</a> documentation.
     * @param photoId The photo ID.
     * @throws QuickrException If there was a problem.
     * @return The photo response.
     */
    public PhotoResponse getInfo(String photoId) throws QuickrException {
        PhotosGetInfoRequest pgir = new PhotosGetInfoRequest(properties.getRestUrl(), properties.getApiKey(), photoId, null, properties.getStaticUrl());
        PhotoResponse photo = pgir.execute();
        
        return photo;
    }    

    /**
     * Add tags to a photo. 
     * For details, see the
     * <a href="http://www.flickr.com/services/api/flickr.photos.addTags.html">
     * flickr.photos.addTags service</a> documentation.
     * @param authToken The auth token.
     * @param photoId The photo ID.
     * @param tags The tags to add.
     * @throws QuickrException If there was a problem.
     */
    public void addTags(AuthResponse.Token authToken, String photoId, Set<String> tags) throws QuickrException {
        PhotosAddTagsRequest patr = new PhotosAddTagsRequest(properties.getRestUrl(), properties.getApiKey(), properties.getSecret(), authToken, photoId, tags);
        patr.execute();
    } 

    /**
     * Get all photo contexts. 
     * For details, see the
     * <a href="http://www.flickr.com/services/api/flickr.photos.getAllContexts.html">
     * flickr.photos.getAllContexts service</a> documentation.
     * @param photoId The photo ID.
     * @throws QuickrException If there was a problem.
     */
    public ContextsResponse getAllContexts(String photoId) throws QuickrException {
        PhotosGetAllContextsRequest pgacr = new PhotosGetAllContextsRequest(properties.getRestUrl(), properties.getApiKey(), photoId);
        ContextsResponse cr = pgacr.execute();
        
        return cr;
    } 

    /**
     * Get photo sizes. 
     * For details, see the
     * <a href="http://www.flickr.com/services/api/flickr.photos.getSizes.html">
     * flickr.photos.getSizes service</a> documentation.
     * @param photoId The photo ID.
     * @throws QuickrException If there was a problem.
     */
    public SizesResponse getSizes(String photoId) throws QuickrException {
        PhotosGetSizesRequest pgsr = new PhotosGetSizesRequest(properties.getRestUrl(), properties.getApiKey(), photoId);
        SizesResponse sr = pgsr.execute();
        
        return sr;
    } 
}
